1   //==============================================================================
2   // file :       SearchFiles.java
3   // project:     Lucene Search System
4   //
5   // last change: date:       $Date: 2003/09/09 03:11:52 $
6   //              by:         $Author: bitiboy $
7   //              revision:   $Revision: 1.1 $
8   //------------------------------------------------------------------------------
9   // copyright:   GNU GPL Software License (see class documentation)
10  //==============================================================================
11  
12  package com.justhis.lucene.xml;
13  
14  
15  /*
16   * $Id: SearchFiles.java,v 1.1 2003/09/09 03:11:52 bitiboy Exp $
17   *
18   * Copyright 2003 Acai Software All Rights Reserved.
19   *
20   * This file LuceneException.java is part of the Lucene Search System.
21  
22   * The Lucene Search System is free software; you can redistribute it and/or modify
23   * it under the terms of the GNU General Public License as published by
24   * the Free Software Foundation; either version 2 of the License, or
25   * (at your option) any later version.
26  
27   * Lucene Search System is distributed in the hope that it will be useful,
28   * but WITHOUT ANY WARRANTY; without even the implied warranty of
29   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30   * GNU General Public License for more details.
31  
32   * You should have received a copy of the GNU General Public License
33   * along with the Lucene Search System; if not, write to the Free Software
34   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35  
36   * http://www.justhis.com http://ejb.cn
37   * CONTACT: email = webmaster@justhis.com superaxis@sohu.com
38   */
39  import com.justhis.lucene.LuceneException;
40  
41  import org.apache.lucene.analysis.Analyzer;
42  import org.apache.lucene.analysis.standard.StandardAnalyzer;
43  import org.apache.lucene.document.Document;
44  import org.apache.lucene.queryParser.ParseException;
45  import org.apache.lucene.queryParser.QueryParser;
46  import org.apache.lucene.search.Hits;
47  import org.apache.lucene.search.IndexSearcher;
48  import org.apache.lucene.search.Query;
49  import org.apache.lucene.search.Searcher;
50  
51  import java.io.IOException;
52  
53  
54  /***
55   * ??????????????????
56   *
57   * @author <a href="http://blog.ejb.cn">acai</a>
58   * @version $Revision: 1.1 $
59   */
60  public class SearchFiles {
61      //~ Methods ----------------------------------------------------------------
62  
63      /***
64       * ????XML????????????????XML????
65       *
66       * @param indexPath
67       * @param keyWords
68       *
69       * @throws LuceneException
70  	 *
71  	 * @todo ????????????????XML??????????????
72  	 * 
73       */
74      public static void search(String indexPath, String keyWords)
75                         throws LuceneException {
76          Searcher searcher = null;
77  
78          try {
79              searcher = new IndexSearcher(indexPath);
80  
81              Analyzer analyzer = new StandardAnalyzer();
82  
83              Query query = QueryParser.parse("title:\"" + keyWords + "\"  "
84                                              + keyWords, "content", analyzer
85                                             );
86              System.out.println("Searching for: " + query.toString());
87  
88              Hits hits = searcher.search(query);
89              System.out.println(hits.length() + " total matching documents");
90  
91              final int HITS_PER_PAGE = 10;
92  
93              for (int start = 0; start < hits.length();
94                       start += HITS_PER_PAGE
95                  ) {
96                  int end = Math.min(hits.length(), start + HITS_PER_PAGE);
97  
98                  for (int i = start; i < end; i++) {
99                      Document doc = hits.doc(i);
100                     String name = doc.get("objectId");
101                     System.out.println(name);
102                     System.out.println(doc.get("author"));
103                     System.out.println(doc.get("title"));
104                 }
105             }
106         } catch (IOException e) {
107             throw new LuceneException(e.getMessage(), e);
108         } catch (ParseException e) {
109             throw new LuceneException(e.getMessage(), e);
110         } finally {
111             if (searcher != null) {
112                 try {
113                     searcher.close();
114                 } catch (IOException e1) {
115                 }
116             }
117         }
118     }
119 }
120 /*
121  * $Log: SearchFiles.java,v $
122  * Revision 1.1  2003/09/09 03:11:52  bitiboy
123  * *** empty log message ***
124  *
125  * Revision 1.1  2003/09/09 00:54:45  bitiboy
126  * *** empty log message ***
127  *
128  * Revision 1.2  2003/09/07 08:45:20  superaxis
129  * ????Search??????????
130  *
131  * Revision 1.1  2003/09/07 08:23:50  superaxis
132  * *** empty log message ***
133  *
134  *
135 */
This page was automatically generated by Maven